<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="false"
    :okAuth="'inspectionmanag:inHiddenDanger:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <div class="fireVeIm">
      <div class="fireImg">
        <span>
          异常照片：
        </span>
        <Image v-if="abnormalPic" :src='abnormalPic' width="100%" height="100%"></Image>
        <img v-else src="/@/assets/images/noData.png" style="width: 150px;height:auto;" alt="">
      </div>

      <div class="fireImg">
        <span>
          隐患处置照片：
        </span>
        <Image v-if='resultPic' :src="resultPic" width="100%" height="100%"></Image>
        <img v-else src="/@/assets/images/noData.png" style="width: 150px;height:auto;" alt="">
      </div>
    </div>
    <BasicForm @register="registerForm" />
    <template #footer>
      <BpmButton
        v-model:bpmEntity="record"
        bpmEntityKey="id"
        formKey="Fire_Hazard_Process"
        completeText="提交"
        :completeModal="true"
        :loading="loadingRef"
        :auth="'inspectionmanag:inHiddenDanger:edit'"
        @validate="handleValidate"
        @complete="handleSubmit"
        @success="handleSuccess"
        @close="closeDrawer"
      />
    </template>
      <BpmRuntimeTraceTimeLine
      :formKey= "'Fire_Hazard_Process'"
      :bizKey=record.taskId
      :maintainTaskId=record.maintenanceTaskId
    />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsInspectionmanagInHiddenDangerForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { InHiddenDanger, inHiddenDangerSave, inHiddenDangerForm } from '/@/api/inspectionmanag/inHiddenDanger';
  import { BpmButton } from '/@/components/Bpm';
  import { Image } from 'ant-design-vue';
  import  BpmRuntimeTraceTimeLine  from '/@/components/Bpm/src/BpmRuntimeTraceTimeLine.vue'; 

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('inspectionmanag.inHiddenDanger');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<InHiddenDanger>({} as InHiddenDanger);
  const loadingRef = ref(false);
  const abnormalPic =ref('')
  const resultPic = ref('')
  // const MaintenanceTaskId= ref('')

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增') : t('详情'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('名称'),
      field: 'name',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true,
      },
    },
    {
      label: t('所属主体'),
      field: 'enterpriseName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true,
      },
    },
    {
      label: t('所属区域'),
      field: 'regionName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true,
      },
    },
    {
      label: t('地点'),
      field: 'address',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true,
      },
    },
    {
      label: t('设备名称'),
      field: 'equipmentName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true,
      },
    },
    {
      label: t('设备Id'),
      field: 'equipmentId',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true,
      },
    },
    {
      label: t('是否设备生成'),
      field: 'isEquipment',
      component: 'Input',
      componentProps:{
        disabled: true,
      }
    },
    {
      label: t('任务状态'),
      field: 'status',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true,
      },
    },
    {
      label: t('隐患描述'),
      field: 'remark',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('上报人'),
      field: 'reportPerson',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('上报时间'),
      field: 'reportTime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
        disabled: true
      },
    },
    // {
    //   label: t('整改期限'),
    //   field: 'deadline',
    //   component: 'Input',
    //   componentProps: {
    //     format: 'YYYY-MM-DD HH:mm',
    //     showTime: { format: 'HH:mm' },
    //     disabled: true
    //   },
    // },
    {
      label: t('报警结果'),
      field: 'alarmResult',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    // {
    //   label: t('是否督促'),
    //   field: 'urged',
    //   component: 'Input',
    //   componentProps: {
    //     disabled: true
    //   },
    // },
    // {
    //   label: t('超期情况'),
    //   field: 'overdue',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 255,
    //     disabled: true
    //   },
    // },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { md: 24, lg: 12 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await inHiddenDangerForm(data);
    // console.log('res', res.inHiddenDanger.maintenanceTaskId);
    // MaintenanceTaskId.value = res.inHiddenDanger.maintenanceTaskId;
    record.value = (res.inHiddenDanger || {}) as InHiddenDanger;
    record.value.__t = new Date().getTime();
    abnormalPic.value = record.value.abnormalPic || '';
    resultPic.value = record.value.resultPic || '';
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleValidate(_event: any, formData: any) {
    try {
      const data = await validate();
      formData(true, data); // 将表单数据传递给 BpmButton
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    }
  }

  async function handleSubmit(event: any) {
    try {
      loadingRef.value = true;
      const data = event?.formData || (await validate()); // 接受 BpmButton 传递过来的表单数据
      data.bpm = Object.assign(data.bpm || {}, record.value.bpm); // 流程信息
      data.status = record.value.status; // 提交状态
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        tid: record.value.tid,
      };
      // console.log('submit', params, data, record);
      const res = await inHiddenDangerSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      loadingRef.value = false;
      setDrawerProps({ confirmLoading: false });
    }
  }
  async function handleSuccess() {
    emit('success');
  }
  
</script>
<style scoped lang="less"> 
.fireVeIm{
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin-bottom: 5%;
}
.fireImg {
  width: 40%;
  height: 200px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
</style>